package HTTPRecieveParser;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import DatabaseManager.MMBoxManager;
import DatabaseManager.TempMSGBox;
import HTTPSendIntegrator.MmsMessageException;

public class ForwardReq {
	
	private String forwardReqMessage;
	
	public static final String MMS_MESSAGE_TYPE = "X-Mms-Message-Type:";
	public static final String MMS_TRANSACTION_ID = "X-Mms-Transaction-ID:";
	public static final String MMS_VERSION = "X-Mms-MMS-Version:";
	public static final String MMS_FROM = "From:"; 
	public static final String MMS_TO = "To:"; 
	public static final String MMS_CC = "Cc:"; 
	public static final String MMS_BCC = "Bcc:";  
	public static final String MMS_CONTENT_LOCATION_VALUE = "X-Mms-Content-Location:";
	public static final String MMS_DATE = "Date:";
	public static final String MMS_SUBJECT = "Subject:";
	public static final String MMS_EXPIRY = "X-Mms-Expiry:";
	public static final String MMS_DELIVERY_TIME = "X-Mms-Delivery-Time:";
	public static final String MMS_REPORT_ALLOWED = "X-Mms-Report-Allowed:";
	public static final String MMS_DELIVERY_REPORT = "X-Mms-Delivery-Report:";
	public static final String MMS_READ_REPLY = "X-Mms-Read-Reply:";
	public static final String MMS_STORE = "X-Mms-Store:";
	
	private String mmsMessageType;
	private String mmsTransactionId;
	private String mmsVersion;
	private String mmsFrom;
	
	private ArrayList<String> mmsTo;
	private ArrayList<String> mmsCc;
	private ArrayList<String> mmsBcc;
	
	private String mmsContentLocationValue;
	
	private String mmsDate;
	private String mmsSubject;
	private String mmsExpiryTime;
	private String mmsDeliveryTime;	
	private Boolean mmsDeliveryReport;
	private Boolean mmsReadReply;
	private Boolean mmsReportAllowed;
	private Boolean mmsStore;
	private String mmsMessageId;

	public Boolean getReadReply() { return this.mmsReadReply; }
	
	public Boolean getReportAllowed() {
		return this.mmsReportAllowed;
	}
	
	public String getMessageType() {
		return mmsMessageType;
	}
	
	public String getTransactionId(){
		return mmsTransactionId;
	}
	
	public String getVersion(){
		return mmsVersion;
	}

	public String getFrom(){
		return mmsFrom;
	}

	public List<String> getTo(){
		return mmsTo;
	}

	public List<String> getCc(){
		return mmsCc;
	}
	
	public List<String> getBcc(){
		return mmsBcc;
	}
	
	public String getContentLocationValue(){
		return mmsContentLocationValue;
	}
	
	public String getMessageID(){
		return this.mmsMessageId;
	}
	
	public String getSubject(){
		return this.mmsSubject;
	}
	
	public String getExpiryTime(){
		return mmsExpiryTime;
	}
	
	public Boolean getStore(){
		return mmsStore;
	}
	
	public ForwardReq(String forwardReqMessage){
		this.forwardReqMessage = forwardReqMessage;
	}
	
	public void decodeMessage() throws MmsMessageException, SQLException {
		
		MMBoxManager mmBoxManager = new MMBoxManager();
		TempMSGBox tempMsgBox = new TempMSGBox();
		
		mmBoxManager.ConnectDB();
		tempMsgBox.ConnectDB();
		
		/**
		 * MeessageId Creation
		 */
		Random mID;
		int ran;
		do{
			mID = new Random(System.currentTimeMillis());
			ran = mID.nextInt();
			if(ran<0)
				ran *= -1;
			mmsMessageId = "MID" + ran;
		}while(mmBoxManager.isExistMsgId(mmsMessageId) &&
				tempMsgBox.isExistMsgId(mmsMessageId));
		
		try{
			String[] result = forwardReqMessage.split("\n");

			for(int i=0; i<result.length; i++)
			{
					if(result[i].startsWith(MMS_MESSAGE_TYPE))
						mmsMessageType=(result[i].substring((result[i].indexOf(':'))+2));
					
					if(result[i].startsWith(MMS_TRANSACTION_ID))
						mmsTransactionId=result[i].substring((result[i].indexOf(':'))+2); 
				
					if(result[i].startsWith(MMS_VERSION))
						mmsVersion=result[i].substring((result[i].indexOf(':'))+2);

					if(result[i].startsWith(MMS_SUBJECT))
						mmsSubject=result[i].substring((result[i].indexOf(':'))+2);
					
					if(result[i].startsWith(MMS_FROM))
						mmsFrom=result[i].substring((result[i].indexOf(':'))+2);
					
					else if(result[i].startsWith(MMS_TO))
						mmsTo.add(result[i].substring((result[i].indexOf(':'))+2));

					else if(result[i].startsWith(MMS_CC))
						mmsCc.add(result[i].substring((result[i].indexOf(':'))+2));
									
					else if(result[i].startsWith(MMS_BCC))
						mmsBcc.add(result[i].substring((result[i].indexOf(':'))+2));
					
					else if(result[i].startsWith(MMS_DATE))
						mmsDate=result[i].substring((result[i].indexOf(':'))+2);

					else if(result[i].startsWith(MMS_EXPIRY))
						mmsExpiryTime=result[i].substring((result[i].indexOf(':'))+2);

					else if(result[i].startsWith(MMS_DELIVERY_TIME))
						mmsDeliveryTime=result[i].substring((result[i].indexOf(':'))+2);

					else if(result[i].startsWith(MMS_REPORT_ALLOWED))
					{
						if(result[i].substring((result[i].indexOf(':'))+2)=="YES")
							this.mmsReportAllowed=true;
						else
							this.mmsReportAllowed=false;
					}
					
					else if(result[i].startsWith(MMS_DELIVERY_REPORT))
					{
						if(result[i].substring((result[i].indexOf(':'))+2)=="YES")
							mmsDeliveryReport=true;
						else
							mmsDeliveryReport=false;
					}
					
					else if(result[i].startsWith(MMS_READ_REPLY))
					{
						if(result[i].substring((result[i].indexOf(':'))+2)=="YES")
							this.mmsReadReply = true;
						else
							this.mmsReadReply = false;
					}
					
					else if(result[i].startsWith(MMS_STORE))
					{
						if(result[i].substring((result[i].indexOf(':'))+2)=="YES")
							this.mmsStore = true;
						else
							this.mmsStore = false;
					}

					else if(result[i].startsWith(MMS_CONTENT_LOCATION_VALUE))
						mmsContentLocationValue=result[i].substring((result[i].indexOf(':'))+2);
			}

		}catch (ArrayIndexOutOfBoundsException e){
			System.out.println("Malformed response message");
		}
	}
	
	public void StoreMMS() throws SQLException{
		MMBoxManager mmBoxManager = new MMBoxManager();
		TempMSGBox tempMsgBox = new TempMSGBox();
		System.out.println("to : "+ mmsTo.size() + " cc : " + mmsCc.size() + " bcc : " + mmsBcc.size());
		
		String[] mm = new String[14];
		String[] to = mmsTo.toArray(new String[mmsTo.size()]);
		String[] cc = mmsCc.toArray(new String[mmsCc.size()]);
		String[] bcc = mmsBcc.toArray(new String[mmsBcc.size()]);
	
		
		mm[0] = mmsContentLocationValue;
		mm[1] = mmsSubject;
		mm[2] = mmsContentLocationValue;//mm[2] = sendReqMsg.  // ContentLocation
		mm[3] = mmsDate;
		mm[4] = mmsExpiryTime;
		mm[5] = mmsDeliveryTime;
		mm[6] = mmsFrom;
		////////////////////////////////////////////////////////////////////////
		//to = sendReqMsg.getTo();
		///////////////////////////////////////////////////////////////////////
		mm[8] = mmsDeliveryReport.toString();
		mm[9] = null;
		mm[10] = null;
		mm[11] = null;
		mm[12] = null;
		mm[13] = null;
		
		if(mmsStore)
		{
			mmBoxManager.ConnectDB();
			mmBoxManager.StoreMessage(mm, to, cc, bcc);
			mmBoxManager.CloseConnectionDB();			
		}else	
		{
			tempMsgBox.ConnectDB();
			tempMsgBox.StoreMessage(mm, to, cc, bcc);
			tempMsgBox.CloseConnectionDB();
		}
	}
}	